package com.example.traffic_learning;

import java.util.ArrayList;

import global.AsyncResponse;
import DTO.FineDTO;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class GetFineDatabaseTask extends AsyncTask<String, Integer, String>{
	Context context;
	public AsyncResponse delegate=null;
	private ProgressDialog progressDialog;
	private boolean isFirst = true;
	public GetFineDatabaseTask(Context context) {
		this.context = context;
	}
	@Override
	protected String doInBackground(String... params) {
		String result = "";
		ArrayList<FineDTO> arrFineDTO = new ArrayList<FineDTO>();
		arrFineDTO = FineDTO.getFineByKeyWord(context, params[0], params[1]);
		result = Law(arrFineDTO);
		return result;
	}
	
	@Override
	protected void onPreExecute(){
		super.onPreExecute();
		if(isFirst()){
			progressDialog = new ProgressDialog(context);
			progressDialog.setTitle("Đang tải dữ liệu....");
			progressDialog.setMessage("Vui lòng đợi!");
			progressDialog.setCancelable(false);
			progressDialog.setIndeterminate(true);
			progressDialog.show();
		}
	}
	
	@Override
	protected void onPostExecute(String result){
		super.onPostExecute(result);
		if(progressDialog != null){
			progressDialog.dismiss();
		}
		delegate.processGetFineDatabaseFinish(result);
	}
	
	public boolean isFirst() {
		return isFirst;
	}
	public void setFirst(boolean isFirst) {
		this.isFirst = isFirst;
	}
	
	private String Law(ArrayList<FineDTO> arrFineDTO){
		String result = "";
		for(int i = 0; i < arrFineDTO.size(); i++){
			result += "<p><strong> Lỗi vi phạm : </strong>";
			result += arrFineDTO.get(i).getContent() + "<br>";
			result += "<strong> Mức phạt : </strong>" + arrFineDTO.get(i).getFine() + "<br>";
			if(!arrFineDTO.get(i).getExtraFines().equals("")){
				result += "<strong> Mức phạt bổ sung : </strong>" + arrFineDTO.get(i).getExtraFines();
			}
			result += "</p>";
		}
		return result;
	}
}
